import { PositionHelper } from "utils/position/PositionHelper";
import { ActionArgHelper } from "../../../ActionManager/ActionArgHelper";

export class ActionUtils
{
    /**
     * 是否要跑路
     * @param creep 当前需要逃跑的爬
     * @param fleeRange 检测creep的范围  fleeRange检测建筑的范围
     * @returns 0 不用跑  1: 有敌人 2: 有建筑里的敌人要出生了
     */
       static isFleeForSourceKeeper(creep:Creep,fleeRange:number=5)
    {
        const creepNextpos = creep.pos;
        //if(creep.hits<creep.hitsMax)
        // creep.room.log('warn',`敌方`,JSON.stringify(creep.room.getHostileCreeps().map(e=>e.name)));
        const roomojbects:RoomObject[] = (creep.room.getSource() as RoomObject[]).concat(creep.room.getMinerial())  ;
 
        {
            const hostiles:RoomObject[] =  creep.room.getHostileCreeps().filter(e=>e.pos.inRangeTo(creepNextpos,fleeRange) && e.body.find(bodyitem=>bodyitem.type==ATTACK || bodyitem.type==RANGED_ATTACK) );
            if(hostiles.length>0)
            {
                const source =_.min (roomojbects,e=>e.pos.distance(creep.pos));
                hostiles.push(source );
                return { 1: hostiles};
            }
        }
        const structures =  creep.room.getStructures(STRUCTURE_KEEPER_LAIR).filter(e=>e.pos.inRangeTo(creepNextpos,fleeRange) && (!e.ticksToSpawn || (e.ticksToSpawn&& e.ticksToSpawn<10))) as RoomObject[];
        if(structures.length>0)
        { 
            const source =_.min (roomojbects,e=>e.pos.distance(creep.pos));
            structures.push(source);
            return {2:structures};
        }
        return 0;
    }
    private static isFleeForCreep(creep:Creep)
    {
        const nextpos = creep.nextPosition();
        const hostiles:RoomObject[] =  creep.room.getHostileCreeps().filter(e=>e.pos.inRangeTo(nextpos,3) && e.body.find(bodyitem=>bodyitem.type==ATTACK || bodyitem.type==RANGED_ATTACK) );
        if(hostiles.length)
        {
            return { 1: hostiles};
        }
        return 0;
    }
    static isFlee(creep:Creep)
    {
        if(creep.room.getType() == 6)
        {
            const path = creep.memory._move
            if( !path || (path.path.length)<6) 
            {
                return   ActionUtils.isFleeForSourceKeeper(creep,5);
            }
            else 
            {
                return  ActionUtils.isFleeForCreep(creep);
            }
        }
        else 
        {
            return ActionUtils.isFleeForCreep(creep);
        }
    }
    /**
     * 9房躲避sourceKeeper
     * @param creep 
     * @returns 
     */
    static getEscapeSourceKeeperAction(creep:Creep,target?:RoomPosition )
    {
        const ret = ActionUtils.isFlee(creep );
             
        if(ret && ret[1])
        {
            creep.say('危!')
            return [ActionArgHelper.actionEscape(creep,ret[1].map(e=>{return {pos:e.pos,range:5}}),{})]
        }
        
        // 在invada附近
        if(ret && ret[2])
        {
            creep.say('跑!')
            return [ActionArgHelper.actionEscape(creep,ret[2].map(e=>{return {pos:e.pos,range:5}}),{})]
        }
        return false
    }
    static getNormalAction(creep:Creep,room:Room)
    {
        // 1.source附近捡资源
         
        const res = room.getResource(RESOURCE_ENERGY);
        if(res.length>0)
        {
            const pickupobj = _.min(res ,e=>
                {
                    if(e.amount<200)
                    {
                        return 50
                    }else if(e.amount<100)
                    {
                        return 100
                    }
                    return e.pos.distance(creep.pos)
                })
            if(pickupobj)
            {
                return [ActionArgHelper.actionPickup(creep,pickupobj)]
            }
        }
        // 2.container里捡资源
        const container = creep.room.getStructures(STRUCTURE_CONTAINER);
        for(const e of  container)
        {
            if( e.store.getUsedCapacity(RESOURCE_ENERGY))
            {
                return [ActionArgHelper.actionWithdraw(creep, e)]
            }
        }

        let sources:Source[]=creep.room.getSource();
        const ret = _.sortBy(sources.filter(e=>e.energy),e=>e.energy* (8-e.pos.getFreeSpace(1).length));
        if(ret.length)
        {
            return [ActionArgHelper.warp(ActionArgHelper.actionHarvest(creep,ret[0]),undefined,
            (arg)=>
            {
                if(Game.getObjectById(ret[0].id)?.energy === 0)
                {
                    return true;
                }
                return arg.arg.entity.store.getFreeCapacity()==0
            })]
        }
        return []
    }
}